package main

import (
	"bufio"
	"fmt"
	"os"
)

func copyFile(oldFile string, newFile string) {
	if oldFile == newFile {
		fmt.Println("文件名and路径相同，copy失败")
		return
	}
	fileObj, err := os.Open(oldFile)
	if err != nil {
		fmt.Println("文件读取失败", err)
		return
	}
	defer fileObj.Close()

	fileObjTow, err2 := os.OpenFile(newFile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
	if err2 != nil {
		fmt.Println("文件创建失败", err2)
		return
	}
	defer fileObjTow.Close()

	scanner := bufio.NewScanner(fileObj)
	for scanner.Scan() {
		line := scanner.Text()

		writer := bufio.NewWriter(fileObjTow)
		_, _ = writer.WriteString(line + "\n")
		_ = writer.Flush()
	}
}

func main() {
	copyFile("./basicDemo/data/MyWrite.txt", "./basicDemo/data/MyWrite(1).txt")
}
